#!/usr/bin/env node
const path = require('path')
const inquirer = require('inquirer');
const fs = require('fs');
const ejs = require('ejs')

inquirer.prompt([
    {
        type: 'input',
        name: 'name',
        message: 'project name? '
    },
    {
        type: 'input',
        name: 'backgroundColor',
        message: 'backgroundColor? '
    }
]).then(answers => {
    console.log(answers);

    // 模板目录
    const tempDir = path.join(__dirname, '../templates')
    // 目标目录
    const destDir = process.cwd()
    // const destDir = path.join(__dirname, '../build')
    console.log(tempDir, destDir)

    // 将模板目录下的文件转换到目标目录
    fs.readdir(tempDir, (err, files) => {
        if (err) throw err
        files.forEach(file => {
            // 通过模板引擎渲染文件
            ejs.renderFile(path.join(tempDir, file), answers, (err, result) => {
                if (err) throw err
                
                fs.writeFileSync(path.join(destDir, file), result)
            })
        })
    })
})